import { indexedDBSettings, DBRes, OptionsType } from "@/const/indexedDB";

const openIndexedDB = () =>
  new Promise((res: (value: DBRes) => void) => {
    const request = window.indexedDB.open(
      indexedDBSettings.databaseName,
      indexedDBSettings.version
    );
    request.onsuccess = () => {
      const db = request.result;
      res({ data: db, code: 0, message: "数据库连接成功" });
    };
    request.onerror = () => {
      res({ code: 1, message: "数据库连接失败" });
    };
    request.onupgradeneeded = (event: any) => {
      const db = event.target.result;
      indexedDBSettings.objectStore.forEach(
        ({ name, keyPath, autoIncrement, createIndexs }) => {
          if (!db.objectStoreNames.contains(name)) {
            let objectStore: any;
            if (autoIncrement) {
              objectStore = db.createObjectStore(name, { autoIncrement });
            } else {
              objectStore = db.createObjectStore(name, { keyPath });
            }
            if (createIndexs) {
              createIndexs.forEach(({ name, value, unique }) => {
                objectStore.createIndex(name, value, { unique });
              });
            }
          }
        }
      );
      res({ data: db, code: 1, message: "数据库升级成功" });
    };
  });

const DBAdd = (db: any, options: OptionsType) =>
  new Promise((res: (value: DBRes) => void) => {
    const request = db
      .transaction([options.name], options.permission)
      .objectStore(options.name)
      .add(options.data);
    request.onsuccess = (e: any) => {
      const data = {
        id: e.target.result
      };
      res({ data, code: 0, message: "数据写入成功" });
    };
    request.onerror = () => {
      res({ code: 1, message: "数据写入失败" });
    };
  });

const DBGet = (db: any, options: OptionsType) =>
  new Promise((res: (value: DBRes) => void) => {
    const { name, permission, data } = options;
    const transaction = db.transaction([name], permission);
    const objectStore = transaction.objectStore(name);
    let request: any;
    if (data.indexName) {
      const index = objectStore.index(data.indexName);
      request = index.get(data.index);
    } else {
      request = objectStore.get(data.index);
    }
    request.onerror = () => {
      res({ code: 1, message: "数据读取失败" });
    };
    request.onsuccess = () => {
      res({ data: request.result, code: 0, message: "数据读取成功" });
    };
  });

// 删除数据
const DBDel = (db: any, options: OptionsType) =>
  new Promise((res: (value: DBRes) => void) => {
    const { name, permission, data } = options;
    const transaction = db.transaction([name], permission);
    const objectStore = transaction.objectStore(name);
    const request = objectStore.delete(data.index);
    request.onsuccess = () => {
      res({ code: 0, message: "数据删除成功" });
    };
  });
// 删除数据库

const delIndexedDB = () =>
  new Promise((res: (value: DBRes) => void) => {
    const request = window.indexedDB.deleteDatabase(
      indexedDBSettings.databaseName
    );
    request.onsuccess = () => {
      res({ code: 0, message: "数据库删除成功" });
    };
    request.onerror = () => {
      res({ code: 1, message: "数据库删除失败" });
    };
    request.onblocked = () => {
      res({ code: 1, message: "数据库删除失败" });
    };
  });

export { openIndexedDB, DBAdd, DBGet, DBDel, delIndexedDB };
